﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SWCad
{
    public partial class HSFDChart : UserControl
    {
        ClassLib clib = new ClassLib();
        String InFileName, OutFileName;
        String[] BatchFileNames;
        enum iInMode:int {iNo=0, iSingle = 1, iBatch=2}; //0 no input; 1 single input; 2 batch input
        int iInFileMode=(int)iInMode.iNo;
        bool bHasDrawChart = false;
        public HSFDChart()
        {
            InitializeComponent();
        }
        
        private void getInPutValue(ClassLib clib)
        {
            textBoxN.Text = clib.iN1.ToString();
            textBoxDT.Text = clib.iDt.ToString();
            textBoxXH.Text = clib.strXH;
            textBoxM.Text = clib.iM1.ToString();            

            if (checkedListBoxPL.Items.Count > 0)
            {
                checkedListBoxPL.Items.Clear();
            }
            for(int i= 0; i<clib.iM1; i++)
            {
                if (1 == clib.iSelect || 3 == clib.iSelect || 4 == clib.iSelect)
                    checkedListBoxPL.Items.Add("P="+clib.dblPL[i]+"%\t"+"|Qm="+clib.dblSQ[i]); //频率+洪峰
                else if(2== clib.iSelect)
                    checkedListBoxPL.Items.Add("P=" + clib.dblPL[i] + "%\t" + "|W=" + clib.dblSW[i]); //频率+洪量
             }
            checkedListBoxPL.SetItemChecked(0, true);
        }

        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Initialize the OpenFileDialog to look for text files:
            BatchAnalyseDlg.Filter = "InPut Files|*.dat";
            // Check if the user selected a file from the OpenFileDialog:
            if (BatchAnalyseDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BatchFileNames = BatchAnalyseDlg.FileNames;
                Int32 iFileNum = BatchFileNames.Length;
                cmbCSNames.Items.Clear();
                for (int i = 0; i < iFileNum; i++)
                {
                    //根据批量输入文件名生成批量输出文件名
                    InFileName = BatchFileNames[i];
                    OutFileName = InFileName.Remove(InFileName.Length - 4) + ".out";

                    CalChart();
                    if(iInFileMode == (int)iInMode.iNo) return;//读取失败，直接跳出
                    //添加站名
                    AddCSName();
                }
                iInFileMode = (int)iInMode.iBatch;

            }
            updateMenu();
        }
        //由输入文件计算图标显示所需数据
        private void CalChart()
        {
            String ConnectFile = InFileName + "*" + OutFileName + "*";
            //获取字节个数，传递给DLL中的函数
            byte[] bit = Encoding.Default.GetBytes(ConnectFile);
            Int32 InLen = bit.Length;

            if (-1 != clib.readHSFD(InFileName))
            {
                clib.HSFD(ConnectFile, InLen); //计算
                clib.readHSFDout(OutFileName);
                getInPutValue(clib);
                iInFileMode = (int)iInMode.iBatch;
            }
            else
            {
                iInFileMode = (int)iInMode.iNo;
            }

            updateMenu();
        }
        //添加控制断面
        private void AddCSName()
        {
            cmbCSNames.Items.Add(clib.strName);
            cmbCSNames.SelectedIndex = cmbCSNames.Items.Count - 1;
        }
        //绘图
        private void DrawChart()
        {
            // Check if a demo module node is selected. Demo module is attached to the node
            // as the Tag property
            clib.HSFD2Plot();
            LineChart LineChart = new LineChart(clib);
            LineChart.Dock = DockStyle.Fill;
            LineChart.Name = "洪水放大";            
            if(ChartControl.Controls.Count>0)
                ChartControl.Controls[0].Dispose(); //刷新图
            ChartControl.Controls.Add(LineChart);
            bHasDrawChart = true;
        }  

        //选择绘制某个频率的成果
        private void checkedListBoxPL_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBoxPL.Items.Count; i++)
            {
                checkedListBoxPL.SetItemChecked(i, false);
            }
            if (checkedListBoxPL.CheckedItems == null)
            {
                checkedListBoxPL.SetItemChecked(checkedListBoxPL.SelectedIndex, false);
            }
            else
            {
                checkedListBoxPL.SetItemChecked(checkedListBoxPL.SelectedIndex, true);
                clib.iPLShow = checkedListBoxPL.SelectedIndex;
            }
            DrawChart();
        }
        private void ChartControl_MouseMoveEvent(System.Object eventSender, AxXtremeChartControl._DChartControlEvents_MouseMoveEvent eventArgs)
        {
            XtremeChartControl.ChartElement Element;
            Element = ChartControl.HitTest(eventArgs.x, eventArgs.y);

            XtremeChartControl.ChartSeriesPoint Point;

            Point = (XtremeChartControl.ChartSeriesPoint)Element;

            if (Point != null)
            {
                System.Diagnostics.Debug.WriteLine("Point " + Point[0]);
            }
        }

        private void Line_Resize(System.Object eventSender, System.EventArgs eventArgs)
        {
 //           menuStrip1.Width = ClientRectangle.Width - 2 * gbOptions.Left;
 //           gbOptions.Width = ClientRectangle.Width - 2 * gbOptions.Left;
            ChartControl.SetBounds(ChartControl.Left, ChartControl.Top, ClientRectangle.Width - 2 * ChartControl.Left+ChartControl.Left, ClientRectangle.Height - ChartControl.Top - 8);
        }

        private void saveChartAsPNGToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                dlgCommonDialogSave.Title = "Save As PNG";
                dlgCommonDialogSave.AutoUpgradeEnabled = true;
                dlgCommonDialogSave.Filter = "Png Files (*.PNG)|*.PNG";
                if (dlgCommonDialogSave.ShowDialog() == DialogResult.OK)
                {
                    if (dlgCommonDialogSave.FileName.Length == 0)
                        return;

                    foreach (object x in ChartControl.Controls[0].Controls)
                    {
                        if (x is AxXtremeChartControl.AxChartControl)
                            ((AxXtremeChartControl.AxChartControl)x).SaveAsImage(dlgCommonDialogSave.FileName, ((AxXtremeChartControl.AxChartControl)x).Width, ((AxXtremeChartControl.AxChartControl)x).Height);
                    }
                }

            }
            catch (Exception es)
            {
                MessageBox.Show(es.Message);
            }
        }

        //选择指定控制断面
        private void cmbCSNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 i = cmbCSNames.SelectedIndex;
            //根据指定控制断面，刷新参数
            if(iInFileMode == (int)iInMode.iBatch)
                InFileName = BatchFileNames[i];
            OutFileName = InFileName.Remove(InFileName.Length - 4) + ".out";
            //批量输入文件生成控制断面列表
            CalChart();
            DrawChart();
        }
        private void updateMenu()
        {
            if((int)iInMode.iNo == iInFileMode)
            {
                exportResultToolStripMenuItem.Enabled = false;
            }
            else if ((int)iInMode.iSingle == iInFileMode || (int)iInMode.iBatch == iInFileMode)
            {
                exportResultToolStripMenuItem.Enabled = true;
            }
            saveChartAsPNGToolStripMenuItem.Enabled = bHasDrawChart;
        }
        //输出计算成果汇总表
        //第一行为各计算频率
        //余下为各断面相应频率下得洪峰流量
        private void exportResultToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Initialize the OpenFileDialog to look for text files:
            saveResDlg.Filter = "Export Files|*.txt";
            // 自动追加后缀
            saveResDlg.AddExtension = true; 
            // Check if the user selected a file from the OpenFileDialog:
            if (saveResDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //实例化一个文件流-->与写入文件相关联
                FileStream ofs = new FileStream(saveResDlg.FileName, FileMode.Create);
                StreamWriter sw = new StreamWriter(ofs);
                string buf=null;

                if(iInFileMode == (int)iInMode.iSingle)
                {
                    clib.readTLJSouttemp(OutFileName + "temp");
                    buf = clib.strBZM;
                    sw.WriteLine(buf);
                    //输出计算频率
                    buf=null;
                    for (int iP = 0; iP < clib.iMp; iP++)
                        buf += clib.dblPP[iP] + "%\t";
                    sw.WriteLine(buf);
                    //输出对应频率的洪峰流量
                    buf=null;
                    for (int iP = 0; iP < clib.iMp; iP++)
                        buf += clib.dblQm[iP]+"\t";
                    sw.WriteLine(buf);
                }
                else if (iInFileMode == (int)iInMode.iBatch)
                {
                    Int32 iFileNum = BatchFileNames.Length;
                    for (int i = 0; i < iFileNum; i++)
                    {
                        InFileName = BatchFileNames[i];
                        OutFileName = InFileName.Remove(InFileName.Length - 4) + ".out";
                        clib.readTLJSouttemp(OutFileName + "temp");
                        cmbCSNames.SelectedIndex = i;                       
                        
                        //第一行输出计算频率
                        buf = null;
                        if(0 == i)
                        {
                            buf = "控制断面\t";
                            for (int iP = 0; iP < clib.iMp; iP++)
                                buf += clib.dblPP[iP] + "%\t";
                            sw.WriteLine(buf);
                        }

                        //输出对应频率的洪峰流量
                        buf = null;
                        buf = cmbCSNames.SelectedItem.ToString() + "\t";
                        for (int iP = 0; iP < clib.iMp; iP++)
                            buf += clib.dblQm[iP]+"\t";
                        sw.WriteLine(buf);
                    }
                }
                sw.Close();
                ofs.Close();
            }
        }

        private void guideToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
